package com.ccx.web.backand;

import com.ccx.model.EbProduct;
import com.ccx.model.EbProductCategory;
import com.ccx.service.EbProductCategoryService;
import com.ccx.service.ProductService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;//低耦合

    @Autowired
    private EbProductCategoryService productCategoryService;

    //1.商品的分页查询方法---分页查询的请求
    //分页：${pageContext.request.contextPath}/product/list?page=当前页-1
    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(value = "page", defaultValue = "1") int page,
                       @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        System.out.println("receive browser proruct page request!!!");
        //1.调用service层的分页查询商品信息的方法
        List<EbProduct> ebProducts = productService.list(page, pageSize);
        //2.返回model(ebProducts)和视图(jsp)
        model.addAttribute("ebProducts", ebProducts);//key-value  你名字---你

        //2.1.传递当前页到product.jsp
        model.addAttribute("curPage", page);
        //2.2 传递最大页到product.jsp
        model.addAttribute("maxPage", ((Page) ebProducts).getPages());

        return "manage/product";
    }

    //2.商品添加的方法---商品添加的请求
    @RequestMapping("/to_save")
    public String toSave(Model model) {
        //查询所有大类和小类
        //1.调用商品分类的service层 查询大类和小类
        Map<String, List<EbProductCategory>> categoryMap = productCategoryService.findBigAndSmallCategory();
        //2.通过model传递给jsp
        model.addAttribute("categoryMap", categoryMap);

        return "manage/product-add";
    }

    //3.删除商品的方法---删除商品的请求
    @RequestMapping("/delect")
    public String delect(int epId) {
        int delect = productService.delect(epId);
        if (delect > 0) {
            return "manage/manage-result";
        }
        return "manage/manage-error";
    }

    //更新
    @RequestMapping("/edit")
    public String edit(EbProduct ebProduct,
                       MultipartFile uploadFile,
                       HttpServletRequest request) {
        System.out.println(ebProduct);
        System.out.println(uploadFile);
        if (uploadFile.getSize() > 0) {
            //0.1 获取上传文件的文件名  1.jpg
            String originalFileName = uploadFile.getOriginalFilename();
            //0.2 生成一个新的文件名  xxxx  + .jpg
            String newFileName = UUID.randomUUID().toString() + originalFileName.substring(originalFileName.indexOf("."));
            //0.3 构建file对象
            //1)获取服务器的images/product 路径ß
            String rootPath = request.getServletContext().getRealPath("/") + "images/product";

            File file = new File(rootPath, newFileName);
            //0.4 将文件上传到服务器images/product
            try {
                uploadFile.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //设置修改的文件的名称
            ebProduct.setEpFileName(newFileName);

        }
        //1.调用service更新商品的方法
        int result = productService.update(ebProduct);
        //2.根据添加的结果(result)跳转页面
        if (result <= 0) {
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }
    //添加商品信息
    //添加商品信息
    @RequestMapping("/add")
    public String addProduct(EbProduct ebProduct, MultipartFile uploadFile, HttpServletRequest request) {
        System.out.println(ebProduct);
        System.out.println(uploadFile);
        if (uploadFile != null) {
            //0.1 获取上传文件的文件名  1.jpg
            String originalFileName = uploadFile.getOriginalFilename();
            //0.2 生成一个新的文件名  xxxx  + .jpg
            String newFileName = UUID.randomUUID().toString() + originalFileName.substring(originalFileName.indexOf("."));
            //0.3 构建file对象
            //1)获取服务器的images/product 路径
            String rootPath = request.getServletContext().getRealPath("/") + "images/product";

            File file = new File(rootPath, newFileName);
            //0.4 将文件上传到服务器images/product
            try {
                uploadFile.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //1.调用商品的service层的添加商品的方法
            //假设所有的商品图片都是1.jpg
            //ebProduct.setEpFileName("1.jpg");
            //1.2 设置上传的文件真实名字
            ebProduct.setEpFileName(newFileName);
            int insert = productService.insert(ebProduct);

            //2.根据添加的结果跳到结果页面
            if (insert <= 0) {
                return "manage/manage-error";
            }
            return "manage/manage-result";
        } else {
            return "manage/manage-error";
        }
    }
    //根据Id查询相对应的商品类型
    @RequestMapping("/to_edit")
    public String editProduct(Model model,int epId){
        EbProduct ebProduct = productService.selectbyId(epId);

        //绑定显示的商品对象
        model.addAttribute("ebProduct",ebProduct);
        //查询所有大类和小类
        //调用商品分类的service层 查询大类和小类
        Map<String, List<EbProductCategory>> categoryMap = productCategoryService.findBigAndSmallCategory();
        //通过model传递给jsp
        model.addAttribute("categoryMap",categoryMap);

        return "manage/product-modify";
    }
}